package cn.itsource.controller;

import cn.itsource.domain.SysUser;
import cn.itsource.service.BlogSysUserService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.BlogConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 控制层
 */
@RestController //所有的方法都返回json格式
@RequestMapping("/login") //公共路径
public class LoginController {
    @Autowired
//    注入service层
    private BlogSysUserService service;

    @PostMapping("/login")
//    接收前端传来的对象(账号,密码),要用@RequestBody注解
    public AjaxResult login(@RequestBody SysUser sysUser, HttpSession session) {
        try {
            SysUser login = service.login(sysUser);
            //2.存到session中
            session.setAttribute(BlogConstant.LOGIN_SESSION, login);
            return AjaxResult.Success(login);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统异常");
        }
    }
}

